package be.bonamis.umpire.ui.client.activities;


import java.util.ArrayList;
import java.util.List;

import be.bonamis.projects.umpire.business.actions.GetMonthsA;
import be.bonamis.projects.umpire.business.actions.GetMonthsR;
import be.bonamis.projects.umpire.business.actions.GetQuizA;
import be.bonamis.projects.umpire.business.actions.GetQuizR;
import be.bonamis.projects.umpire.business.actions.GetQuizResultA;
import be.bonamis.projects.umpire.business.actions.GetQuizResultR;
import be.bonamis.projects.umpire.business.actions.GetQuizStatsA;
import be.bonamis.projects.umpire.business.actions.GetQuizStatsR;
import be.bonamis.umpire.ui.client.ClientFactory;
import be.bonamis.umpire.ui.client.mvp.QuizView;
import be.bonamis.umpire.ui.client.places.DispoPlace;
import be.bonamis.umpire.ui.client.places.QuizPlace;
import be.bonamis.umpire.ui.client.places.WhoPlace;
import be.credoc.common.gwt.actions.client.ActionServiceWrapper;
import be.credoc.common.gwt.actions.client.OnResponseCommand;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.CheckBox;

public class QuizActivity extends BaseActivity<QuizPlace> implements QuizView.Presenter {

	private QuizView quizView;
	private ActionServiceWrapper asw;

	public QuizActivity(QuizPlace place, ClientFactory clientFactory) {
		super(place, clientFactory);
		this.clientFactory = clientFactory;
		this.asw = clientFactory.getActionServiceWrapper();
		this.place = place;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		quizView = clientFactory.getQuizView();
		if (place.getUmpire() != null) {
			quizView.setValues(place.getUmpire(), place.getQuizs());
		}
		quizView.setPresenter(this);
		panel.setWidget(quizView.asWidget());
	}

	@Override
	public void getQuizById(long id) {		
		GetQuizA<GetQuizR> action = new GetQuizA<GetQuizR>();
		action.setId(id);
		asw.execute(action, new OnResponseCommand<GetQuizR>() {
			@Override
			public void execute() {
				quizView.setQuiz(response.getQuizDTO());
				putResponsesInDB(quizView.getUmpire().getId(), quizView.getQuiz().getId(), null);				
			}
		});
		
	}

	@Override
	public void showStatsTable(long id) {
		GetQuizStatsA<GetQuizStatsR> action = new GetQuizStatsA<GetQuizStatsR>();
		action.setId(id);
		asw.execute(action, new OnResponseCommand<GetQuizStatsR>() {
			@Override
			public void execute() {
				quizView.setQuizStats(response.getQuizDTO());				
			}
		});		
	}

	@Override
	public void putResponsesInDB(Integer personId, long quizId, List<CheckBox> checkboxes) {
		GetQuizResultA<GetQuizResultR> action = new GetQuizResultA<GetQuizResultR>();
		action.setPersonId(personId);
		action.setQuizId(quizId);
		if (checkboxes != null) {
			List<Boolean> responses = new ArrayList<Boolean>();
			for (CheckBox checkBox : checkboxes) {
				responses.add(checkBox.getValue());
			}		
			action.setResponses(responses);
		}		
		asw.execute(action, new OnResponseCommand<GetQuizResultR>() {
			@Override
			public void execute() {
				if (response != null) {
					quizView.setPersonResult(response.getPrDTO());
					System.out.println();
				}								
			}
		});		
	}
	
	@Override
	public void dispoButtonClicked() {
		GetMonthsA<GetMonthsR> action = new GetMonthsA<GetMonthsR>();
		asw.execute(action, new OnResponseCommand<GetMonthsR>() {
			@Override
			public void execute() {
				clientFactory.getPlaceController().goTo(
						new DispoPlace("dispo", quizView.getUmpire(), response.getSetDates()));
			}
		});

	}

	@Override
	public void whoButtonClicked() {
		clientFactory.getPlaceController().goTo(new WhoPlace("who", quizView.getUmpire()));
	}
	
}
